import { ObjectBaseInterface } from "@/api/type";
import { modelFormInterface } from "@/components/formEncapsulation/dialogForm.vue";
import { searchObjectInterface } from "@/components/formEncapsulation/searchIndex.vue";
import { Arrayable } from "element-plus/es/utils/typescript.mjs";
import { FormItemRule } from "element-plus/lib/components/index.js";

/**
 * @description 科学计数法
 */
const scientificToDecimal = (value: string | number, precision = 2) => {
    // 确保输入是一个字符串或数字
    if (isNaN(Number(value))) {
        return "Invalid number format";
    }

    // 将输入转换为字符串，方便检查是否含有 'e'
    const strValue = String(value);

    // 判断是否已经是科学计数法格式
    if (strValue.includes('e') || strValue.includes('E')) {
        // 如果是科学计数法，转换为普通数字格式
        return Number(value).toLocaleString("fullwide", { useGrouping: false });
    } else {
        // 如果不是科学计数法，转换为科学计数法格式
        return Number(value).toExponential(precision);
    }
}

/**
 * @description 处理参数传递
 */

const paramsSendAxios = (type: string, exct: ObjectBaseInterface | ObjectBaseInterface[] | searchObjectInterface, oldExce?: ObjectBaseInterface) => {
    const baseParam = {
        requestType: "",
        requestId: "",
        body: {
            informosomeId: "",
            namespace: "",
            content: {} as Record<string, unknown>
        },
        modules: [{
            informosomeId: "",
            namespace: "",
            content: {}
        }],
        conditions: [{}]
    }
    baseParam.modules = []
    baseParam.conditions = []
    switch (type) {
        case 'get':
            if (!Array.isArray(exct)) {
                for (const i in exct) {
                    if (typeof exct[i] == 'object')
                        if (exct[i]?.['value'] && exct[i]?.['like']) {
                            baseParam.body.content[i] = `%${exct[i]?.['value']}%`;
                        } else if (exct[i]?.['value'] && !exct[i]?.['like']) {
                            baseParam.body.content[i] = `${exct[i]?.['value']}`;
                        }
                }
            }
            break
        case 'add':
            baseParam.modules.push({
                informosomeId: "",
                namespace: "",
                content: exct
            })
            break
        case 'update':
            baseParam.modules.push({
                informosomeId: "",
                namespace: "",
                content: exct
            })
            baseParam.conditions.push(oldExce as ObjectBaseInterface)
            break;
        case "delete":
            if (!Array.isArray(exct)) {
                baseParam.conditions.push(exct)
            } else {
                baseParam.conditions = exct
            }
            break
        case "copy":
            baseParam.modules.push({
                informosomeId: "",
                namespace: "",
                content: exct
            })
            baseParam.conditions.push(oldExce as ObjectBaseInterface)
            break
    }
    return baseParam
}


/**
 * @description 处理表单验证
 */
const submitRules = (data: modelFormInterface[]) => {
    const rules: Partial<Record<string, Arrayable<FormItemRule>>> = {};
    data.forEach(item => {
        if (item.type == 'input') {
            rules[item.prop] = [
                { required: true, message: item.name + "不能为空", trigger: "blur" },
                ...(item.rules || [])
            ]
        } else if (item.type == 'select') {
            rules[item.prop] = [
                { required: true, message: item.name + "不能为空", trigger: "change" },
                ...(item.rules || [])
            ]
        } else if (item.type == 'input_number') {
            rules[item.prop] = [
                { required: true, message: item.name + "不能为空", trigger: "blur" },
                ...(item.rules || [])
            ]
        }
    })
    return rules
}

export { scientificToDecimal, paramsSendAxios, submitRules }